package logic

import (
	"context"
	"fmt"
	"github.com/google/uuid"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
	"order/crud"
	"order/utils/alipay"

	"order/internal/svc"
	"order/order"

	"github.com/shopspring/decimal"
	"github.com/zeromicro/go-zero/core/logx"
)

type CreateOrderLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCreateOrderLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateOrderLogic {
	return &CreateOrderLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CreateOrderLogic) CreateOrder(in *order.CreateOrderRequest) (*order.CreateOrderResponse, error) {
	// todo: add your logic here and delete this line
	// TODO: 计算商品金额
	// TODO: 生成订单id并记录订单为待支付状态
	orderID := uuid.NewString()
	price := decimal.NewFromInt(20)

	orderMod := crud.NewOrderModel(crud.Client)
	_, err := orderMod.Insert(l.ctx, &crud.Order{
		Uid:     1,
		Amount:  200,
		State:   0,
		OrderNo: in.OrderNo,
	})
	if err != nil {
		return nil, err
	}

	url, err := alipay.Pay(orderID, price.String())
	if err != nil {
		return nil, err
	}

	fmt.Println("接口执行完了****************************************************************")
	return &order.CreateOrderResponse{
		URl: url,
	}, status.Error(codes.Aborted, "test")
}
